﻿
$.extend($.fn.validatebox.defaults.rules, {
    equalTo: {
        validator: function (value, param) {
            return $(param[0]).val() == value;
        },
        message: '输入不一致'
    },
    phoneNum: { //验证手机号   
        validator: function (value, param) {
            return /^1(3|4|5|7|8)\d{9}$/.test(value);
        },
        message: '请输入正确的手机号码。'
    },
    telNum: { //既验证手机号，又验证座机号
        validator: function (value, param) {
            return /(^(0[0-9]{2,3}\-)?([2-9][0-9]{6,7})+(\-[0-9]{1,4})?$)|(^((\d3)|(\d{3}\-))?(1[34578]\d{9})$)/.test(value);
        },
        message: '请输入正确的电话号码。'
    },
    starteEail: { //验证email前缀
        validator: function (value, param) {
            return /(^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]$)/.test(value);
        },
        message: '请输入正确的邮箱地址。'
    }
});

/*布局部分*/
$('#theme-login-layout').layout({
    fit: true /*布局框架全屏*/
});

/*表单定义*/
$('#usertypeselect').combobox({
    editable: false, /*不允许用户通过输入选择*/
    onChange: function (newValue, oldValue) {
        $("#usertype").val(newValue);
    }
});

var usertypeselect = $('#usertypeselect').combobox('panel');
usertypeselect.panel({ cls: "usertypeselect-panel" });

$('#email').textbox({
    prompt: '邮箱',
    required: true,
    missingMessage: "请输入邮箱名称"
});

$('#ages').numberspinner({
    type: "年龄",
    prompt: '年龄',
    required: true,
    missingMessage: "输入年龄",
    min: 0,
    editable: true
});

$('#emailselect').combobox({
    editable: false
});

$('#sexsselect').combobox({
    editable: false,
    onChange: function (newValue, oldValue) {
        $("#sexs").val(newValue);
    }
});

/*获取省份*/
$('#provinceselect').combobox({
    url: '/EasyUI/Admin/GetAreaList?levels=1&parentid=0',
    valueField: 'CityCode',
    textField: 'AreaName',
    editable: false,
    onChange: function (newValue, oldValue) {
        $('#cityselect').combobox({
            url: '/EasyUI/Admin/GetAreaList?levels=2&parentid=' + newValue,
            valueField: 'CityCode',
            textField: 'AreaName',
            editable: false
        });

        $('#townselect').combobox({
            editable: false
        }); 
    }
});

/*获取城市*/
$('#cityselect').combobox({
    editable: false,
    onChange: function (newValue, oldValue) {
        $('#townselect').combobox({
            url: '/EasyUI/Admin/GetAreaList?levels=3&parentid=' + newValue,
            valueField: 'CityCode',
            textField: 'AreaName',
            editable: false
        }); 
    }
});

/*获取县城*/
$('#townselect').combobox({
    editable: false,
    onChange: function (newValue, oldValue) {
        $("#addressinfo").val($("#provinceselect").combobox("getText") + $("#cityselect").combobox("getText") + $("#townselect").combobox("getText") + $(this).combobox("getText"));
        $("#addressid").val(newValue);
    }
}); 

var mailselect = $('#emailselect').combobox('panel');
mailselect.panel({ cls: "theme-mail-select-panel" });

var sexsselect = $('#sexsselect').combobox('panel');
sexsselect.panel({ cls: "theme-mail-select-panel" });

var provinceselect = $('#provinceselect').combobox('panel');
provinceselect.panel({ cls: "theme-mail-select-panel" });

var cityselect = $('#cityselect').combobox('panel');
cityselect.panel({ cls: "theme-mail-select-panel" });

var townselect = $('#townselect').combobox('panel');
townselect.panel({ cls: "theme-mail-select-panel" });

$('#username').textbox({
    prompt: '用户名',
    required: true,
    missingMessage: "请输入用户名"
});

$('#passwords').textbox({
    type: "密码",
    prompt: '密码',
    required: true,
    missingMessage: "请输入密码"
});

$('#passwordscomfirm').textbox({
    type: "密码",
    prompt: '确认密码',
    required: true,
    missingMessage: "输入密码不匹配"
});

$('#phones').textbox({
    type: "手机号码",
    prompt: '手机号码',
    required: true,
    missingMessage: "输入手机号码"
});

$('#codes').textbox({
    required: true,
    missingMessage: "请输入验证码",
    buttonText: '重新获取验证码',
    prompt: '验证码',
    onClickButton: function () {
        ChangeImage();
    }
});

$('#login').form({
    url: "/EasyUI/Admin/CheckLogin",
    onSubmit: function () {
        if ($("#usertype").val() === "") {
            $.messager.alert('温馨提示', "请选择用户类型！", 'error');
            return false;
        }
        var res = $(this).form('enableValidation').form('validate');
        if (res) { 
            $("#useremail").val($("#email").val() + $("#emailselect").find("option:selected").text());
            $.messager.progress();
        }
        return res;
    },
    success: function (result) { 
        if (typeof (result) !== "object")
            result = JSON.parse(result);
        $.messager.progress('close');
        if (result !== null && typeof (result) === "object") {
            if (result.code === 200) {
                OperateMsg("授权提示", result.msg);
            }
            else if (result.code === 401)
                $.messager.alert('授权提示', result.msg, 'error');
            else if (result.code === 402) {
                ChangeImage();
                $.messager.alert('异常提醒', result.msg, 'error');
            }
            else
                $.messager.alert('异常提醒', '系统异常', 'error');
        }
        else
            $.messager.alert('异常提醒', '系统异常', 'error');
    },
    error: function () {
        $.messager.alert('异常错误', '异常错误', 'error');
    }
});

$('#register').form({
    url: "/EasyUI/Admin/RegisterCheck",
    onSubmit: function () {
        if ($("#usertype").val() === "") {
            $.messager.alert('温馨提示', "请选择用户类型！", 'error');
            return false;
        }
        if ($("#sexs").val() === "" || $("#sexs").val() === "0") {
            $.messager.alert('温馨提示', "请选择性别！", 'error');
            return false;
        }
        var res = $(this).form('enableValidation').form('validate');
        if ($("#provinceselect").val() === "请选择" && $("#cityselect").val() === "请选择" && $("#townselect").val() === "请选择") {
            $.messager.alert('温馨提示', "请填写地址信息！", 'error');
            return false;
        }
        if (res) {
            $("#useremail").val($("#email").val()+ $("#emailselect").find("option:selected").text());
            $.messager.progress();
        }
        return res;
    },
    success: function (result) {
        if (typeof (result) !== "object")
            result = JSON.parse(result);
        $.messager.progress('close');
        if (result !== null && typeof (result) === "object") {
            if (result.code === 200) {
                OperateMsg("授权提示", result.msg);
            }
            else if (result.code === 401)
                $.messager.alert('授权提示', result.msg, 'error');
            else if (result.code === 402) {
                ChangeImage();
                $.messager.alert('异常提醒', result.msg, 'error');
            }
            else
                $.messager.alert('异常提醒', '系统异常', 'error');
        }
        else
            $.messager.alert('异常提醒', '系统异常', 'error');
    },
    error: function () {
        $.messager.alert('异常错误', '异常错误', 'error');
    }
});

//刷新验证码
function ChangeImage() {
    var code = $("#img").attr("src");
    $("#img").attr("src", code + "1");
}
